package com.jhgk.jhcee.expert.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhgk.jhcee.auth.mapper.MajorRecruitMapper;
import com.jhgk.jhcee.auth.model.MajorRecruit;
import com.jhgk.jhcee.auth.model.MajorRecruitExample;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import com.jhgk.jhcee.common.resultful.PageResultful;
import com.jhgk.jhcee.expert.domain.dao.MajorRecruitDao;
import com.jhgk.jhcee.expert.domain.model.MajorRecruitMode;
import com.jhgk.jhcee.expert.service.MajorRecruitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/10 9:37.
 */
@Service(value = "MajorRecruitServiceImpl")
public class MajorRecruitServiceImpl implements MajorRecruitService {

    @Autowired
    MajorRecruitMapper majorRecruitMapper;

    @Autowired
    MajorRecruitDao majorRecruitDao;

    @Override
    public PageResultful<PageInfo<MajorRecruit>> search(MajorRecruitMode majorRecruitMode) {
        if (null == majorRecruitMode) {
            return PageResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        MajorRecruitExample example = new MajorRecruitExample();
        MajorRecruitExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceUuidEqualTo(majorRecruitMode.getProvinceUuid());
        criteria.andYearEqualTo(majorRecruitMode.getYear());
        PageHelper.startPage(majorRecruitMode.getPage(), majorRecruitMode.getLimit());
        List<MajorRecruit> majorRecruits = majorRecruitMapper.selectByExample(example);
        if (majorRecruits.isEmpty()) {
            return PageResultful.fail(BasisResultEnum.QUERY_IS_NULL);
        }
        PageInfo<MajorRecruit> pageInfo = new PageInfo<>(majorRecruits);
        return PageResultful.success(pageInfo);
    }

    @Override
    public BasisResultful<Object> searchAllSize(String provinceUuid, Integer year) {
        if (StringUtils.isBlank(provinceUuid)) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        MajorRecruitExample example = new MajorRecruitExample();
        MajorRecruitExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceUuidEqualTo(provinceUuid);
        criteria.andYearEqualTo(year);
        PageHelper.startPage(1, 1);
        List<MajorRecruit> majorRecruits = majorRecruitMapper.selectByExample(example);
        if (majorRecruits.isEmpty()) {
            return BasisResultful.fail(BasisResultEnum.QUERY_IS_NULL);
        }
        PageInfo<MajorRecruit> pageInfo = new PageInfo<>(majorRecruits);
        return BasisResultful.success(pageInfo.getTotal());
    }

    @Override
    public BasisResultful<Object> delete(MajorRecruit majorRecruit) {
        return null;
    }

    @Override
    public BasisResultful<Object> updateBatch(List<MajorRecruit> majorRecruits) {
        return null;
    }

    @Override
    public BasisResultful<Object> addBatch(List<MajorRecruit> majorRecruits) {
        return null;
    }

    @Override
    public BasisResultful<Object> updateMajorRecruit(String provinceUuid, Integer year) {
        if (StringUtils.isBlank(provinceUuid) || null == year) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        Integer updateStatus = majorRecruitDao.setLowstScoreNull(provinceUuid, year);
        if (updateStatus > 0) {
            return BasisResultful.success(updateStatus);
        }
        return BasisResultful.fail();
    }
}
